package com.cqc.jdk8.Jtime;

import org.junit.Test;

import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.Locale;

/**
 * @author cui
 * @date 2024/6/29 19:39
 * @description
 **/
public class LocalTimeTest {

    @Test
    public void getField() {
        LocalTime time = LocalTime.now();
        int hour = time.getHour();
        int minute = time.getMinute();
        int second = time.getSecond();
        System.out.println("time=" + time);
        //22:37:26
        System.out.println(hour + ":" + minute + ":" + second);
    }

    //格式化
    @Test
    public void format() {

        LocalTime time = LocalTime.now();
        System.out.println("toString=" + time.toString());//22:08:38.067
        System.out.println("ISO_TIME=" + time.format(DateTimeFormatter.ISO_TIME));//21:59:45.264
        System.out.println("ISO_LOCAL_TIME=" + time.format(DateTimeFormatter.ISO_LOCAL_TIME));//21:59:45.264
        System.out.println("自定义格式=" + time.format(DateTimeFormatter.ofPattern("HH:mm:ss")));//21:59:45

    }

    //解析
    @Test
    public void parse() {

        //注意：toString()末尾会省略00，如果yMHms改成中文就不会省略
        LocalTime time = LocalTime.parse("10:30:00", DateTimeFormatter.ofPattern("HH:mm:ss"));
        System.out.println(time.toString());//10:30
        time = LocalTime.parse("10:30:00", DateTimeFormatter.ofPattern("HH:mm:ss", Locale.CHINA));
        System.out.println(time.toString());//10:30


        time = LocalTime.parse("12:05", DateTimeFormatter.ofPattern("HH:mm"));
        System.out.println(time.toString());//12:05

    }
}
